package com.cbwl.eoas.cms.service.impl.sysconfig;



import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cbwl.eoas.cms.api.sysconfig.ISysUserRoleService;
import com.cbwl.eoas.cms.dao.sysconfig.ISysUserDao;
import com.cbwl.eoas.cms.dao.sysconfig.ISysUserRoleDao;
import com.cbwl.eoas.cms.model.sysconfig.SysUserDTO;
import com.cbwl.eoas.cms.model.sysconfig.SysUserRoleDTO;
import com.cbwl.eoas.common.base.BaseServiceImpl;
import com.cbwl.eoas.common.util.Md5Util;
import com.cbwl.eoas.common.util.constain.DicDetailConstant;
import com.cbwl.eoas.common.util.jpush.JPush;
import com.cbwl.eoas.common.util.jpush.JPushData;


/**
 * 
 * <p>[描述信息：SysUserRoleDAO接口]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月12日 下午13:29:23
 */
@Service
public class SysUserRoleServiceImpl  extends BaseServiceImpl<SysUserRoleDTO>  implements ISysUserRoleService  {

	
	@Autowired
	private ISysUserRoleDao sysUserRoleDao;
	
	@Autowired
	private ISysUserDao isysUserDao;
	
	@Autowired
	private JPush jPush;
	
	
	
	@Override
	public Object savaOrUpdateUserRole(SysUserRoleDTO bean) throws Exception {
		
		if(StringUtils.isEmpty(bean.getIdStr())){
			return 0;
		}
		//根据用户id删除记录
		sysUserRoleDao.deleteURoleByUid(bean);
		String[] arrIdStr = bean.getIdStr().split(",");
		SysUserRoleDTO surean = new SysUserRoleDTO();
		surean.setUid(bean.getUid());
		for(String id:arrIdStr){
			surean.setRid(Long.valueOf(id));
			sysUserRoleDao.saveObject(surean);
		}
		
		SysUserDTO sbean = new SysUserDTO();
		sbean.setId(bean.getUid());
		sbean = isysUserDao.getObjectById(sbean);
		
		//发送推送，告知终端权限编号
		JPushData jPushData =  new JPushData();
		jPushData.setCode(DicDetailConstant.jpush_type_2);
		jPushData.setContent("您的菜单权限有变化，请查看");
		List<String> alias =  new ArrayList<String>();
		alias.add(Md5Util.encodePassword(sbean.getUserJobno(),JPush.SALT));
		jPushData.setAlias(alias);
		
		jPush.pushWithAliasByMessage(jPush.getUserClient(),jPushData);
		
		
		return arrIdStr.length;
	}
	
	
}
